package com.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.mapper.ProductMapper;
import com.product.pojo.Product;
import com.product.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private final ProductMapper productMapper;

    public ProductServiceImpl(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    @Override
    public Product addProduct(Product product) {
        productMapper.insert(product);
        return product;
    }

    @Override
    public Product updateProduct(Product product) {
        productMapper.updateById(product);
        return product;
    }

    @Override
    public void deleteProduct(String productId) {
        productMapper.deleteById(productId);
    }

    @Override
    public Product getProductById(String productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public List<Product> listAllProducts() {
        return productMapper.selectList(null);
    }

    @Override
    public List<Product> listProductsByCategory(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        return productMapper.selectList(queryWrapper);
    }

    @Override
    public void onShelf(String productId) {
        Product product = getProductById(productId);
        product.setIsOnShelf(1);
        updateProduct(product);
    }

    @Override
    public void offShelf(String productId) {
        Product product = getProductById(productId);
        product.setIsOnShelf(0);
        updateProduct(product);
    }
}